// pages/studyRecord/grammar/grammar.js  语法查看错题界面

//获取应用实例
const app = getApp();
const httpUtils = require('../../../utils/httpUtils.js');
const CryptoJS = require('../../../utils/CryptoJS.js');

var list = [{
  "id": 121481,
  "translation": "柔韧的",
  "title": "flexible",
  "voice": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/flexible.mp3"
},
{
  "id": 121482,
  "translation": "今天是周日",
  "title": "Today is sundy",
  "voice": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/flexible.mp3"
},
{
  "id": 121483,
  "translation": "有志者，事竟成",
  "title": "Where there is a will,there is a way.",
  "voice": "https://yixuepai-media.oss-cn-beijing.aliyuncs.com/Public/uploads/england/flexible.mp3"
}
];
Page({

  /**
   * 页面的初始数据
   */
  data: {
    grammarlist: [],
    type: 4, //	 4语法 
    page: 1,
    pageSize: 50,
    reviewLen: 0, //复习的长度
    date: '',
    hasreview: 0, //0未复习，1已提交复习
    innerAudioContext: new Object(),
    audioTotalsrc: "https://vediosource.oss-cn-beijing.aliyuncs.com/grammar/US/voice/",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this;
    that.setData({
      date: options.date,
    })
    console.log("grammar查看错题界面：", that.data.date);
    //初始化audio
    that.data.innerAudioContext = wx.createInnerAudioContext();
    that.data.innerAudioContext.autoplay = false;
    that.data.innerAudioContext.loop = false;

    //获取复习的语法的错题句子列表
    that.getDatas();
  },

  //获取语法数据列表
  getDatas: function () {
    let that = this;
    wx.showLoading({
      title: '正在加载',
      mask: true
    })

    httpUtils.lookError({
      access_token: app.globalData.studentInfo.access_token ? app.globalData.studentInfo.access_token : app.globalData.access_token,
      day: that.data.date,  
    }).then((res) => {
      // status 0 英文 
      //假数据
      // let temparr = that.data.grammarlist;
      let temparr = res.data.list;
      for (let i = 0; i < temparr.length; i++) {
        temparr[i].status = 0;
        temparr[i].click = 0;
        temparr[i].fontw = 'normal'
      }
      that.setData({
        grammarlist: temparr,
      })

    });
  },

  /**
   * 点击语法的句子列表
   */
  playGrammarVoice: function (e) {
    let that = this;
    let tmp = new Object;
    tmp = that.data.grammarlist;
    let hasClick = that.data.reviewLen;
    this.data.innerAudioContext.stop();
    let idx = e.currentTarget.dataset.idx;
    let courseid = e.currentTarget.dataset.courseid + "";
    let chapterid = e.currentTarget.dataset.chapterid + "";
    let sentenceid = e.currentTarget.dataset.id + "";
    let idstr = courseid + chapterid + sentenceid;
    console.log("idstr:" + idstr);
    let encodeStr = CryptoJS.SHA256(idstr).toString();
    that.data.innerAudioContext.src = that.data.audioTotalsrc + encodeStr + ".wav"
    console.log("videosrc:" + that.data.innerAudioContext.src)
    this.data.innerAudioContext.play();

    console.log("idx:", idx);
    if (tmp[idx].click == 0) {
      tmp[idx].fontw = 600;
    } else if (tmp[idx].click == 1) {
      tmp[idx].bgcolor = "#D6FFE7";
      tmp[idx].status = 1;
      hasClick++;
    } else if (tmp[idx].click == 2) {
      if (tmp[idx].status == 1) {
        tmp[idx].status = 0;
      } else {
        tmp[idx].status = 1;
      }
    }
    if (tmp[idx].click < 2) {
      tmp[idx].click = parseInt(tmp[idx].click) + 1;
    }
    that.setData({
      grammarlist: tmp,
      reviewLen: hasClick
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //停止播放
    let that = this;
    that.data.innerAudioContext.stop();
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //停止播放
    let that = this;
    that.data.innerAudioContext.stop();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})